﻿using Newtonsoft.Json;
using PCS.DTO;
using RabbitMQShare;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PCS
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static ViewModel data = new ViewModel();
        private static RabbitMqModel _rabbitMqModel;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = data;
            grid = this.testDataGrid;
            _rabbitMqModel = new RabbitMqModel()
            {
                Host = "localhost",
                Port = 5672,
                UserName = "guest",
                PassWord = "guest",
                QueueName = "testQue1",
                ExchangeName = "testExchange"
            };
            RabbitMQUtil.InitFactory(_rabbitMqModel);
            Consumer._connection = RabbitMQUtil._connectionFactory.CreateConnection();
            Consumer._channel = Consumer._connection.CreateModel();
            // 开启消费者服务
            Consumer.ConsumerMessage(_rabbitMqModel.QueueName);
            // 模拟生成任务
            CreateTaskInfo();

        }
        public static DataGrid grid;

        public static void RefreshDataGrid()
        {
            grid.Items.Refresh();
        }

        private async Task CreateTaskInfo()
        {
            // 创建十个线程,分别创建任务
            for (int i = 0; i < 10; i++)
            {
                Thread thread = new Thread(CreateLevelTask);  // 
                thread.Start();
                await Task.Delay(500);
                ii++;
            }
        }
        static int ii = 0;
        static Random random = new Random();
        private void CreateTask()
        {
            var message = new TaskInfoDTO
            {
                Task_Id = 1 + random.Next(0,1000),
                MATENUMBER = "st-1234523123" + random.Next(0, 1000),
                OPERNUMBER = "GX005",
                WORKORDERNUMBER = "S12345",
                QTY = 8,
                State = 1,
                Bgein_Date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"),
                End_Date = "",
                Remark = "",
                Create_Date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"),
                Create_UserId = "admin",
                Bar_Code = "#dda1245",
                AGV_OrderId = 1879,
                BindingOrder = "0"
            };
            bool result = RabbitMQUtil.SendMsgToServer(message, _rabbitMqModel.QueueName, _rabbitMqModel.ExchangeName,LogUtils.log);
            if (result)
            {
                this.Dispatcher.Invoke(() =>
                {
                    data.TaskInfos.Add(message);
                });
                RabbitMQUtil._keys.Add(message.Task_Id);
            }
            else
                this.Dispatcher.Invoke(() =>
                {
                    MessageBox.Show("发送消息失败");
                });
            Thread.Sleep(1000);
            //while (true)
            //{

            //}
        }


        // 有优先级的消息发送
        private void CreateLevelTask()
        {
            var message = new TaskInfoDTO
            {
                Task_Id = 1 + random.Next(0, 1000),
                MATENUMBER = "st-1234523123" + random.Next(0, 1000),
                OPERNUMBER = "GX005",
                WORKORDERNUMBER = "S12345",
                QTY = 8,
                State = 1,
                Bgein_Date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"),
                End_Date = "",
                Remark = "",
                Create_Date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"),
                Create_UserId = "admin",
                Bar_Code = "#dda1245",
                AGV_OrderId = 1879,
                BindingOrder = "0"
            };
            if (ii == 3 || ii==5 || ii==7)
            {
                message.Remark = "vip";
            }
            bool result = RabbitMQUtil.SendMsgToServerWithLevel(message, _rabbitMqModel.QueueName, _rabbitMqModel.ExchangeName,LogUtils.log);
            if (result)
            {
                this.Dispatcher.Invoke(() =>
                {
                    data.TaskInfos.Add(message);
                });
                RabbitMQUtil._keys.Add(message.Task_Id);
            }
            else
                this.Dispatcher.Invoke(() =>
                {
                    MessageBox.Show("发送消息失败");
                });
            Thread.Sleep(1000);
        }

        public async Task test()
        {
            await Task.Delay(2000);
            data.TaskInfos.RemoveAt(4);
            testDataGrid.Items.Refresh();
        }

        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //await test();
        }
        private void TCurveDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }
        //private void TCurveDataGrid_PreviewKeyUp(object sender, KeyEventArgs e)
        //{
        //    if (testDataGrid.SelectedIndex < 0) return;
        //    var dgr = (DataGridRow)(testDataGrid.ItemContainerGenerator.ContainerFromIndex(testDataGrid.SelectedIndex));
        //    if (e.Key == Key.Delete && !dgr.IsEditing)
        //    {
        //        testDataGrid.Items.Refresh();
        //    }
        //}

        //private void testDataGrid_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        //{
            
        //    testDataGrid.Items.Refresh();
        //}
    }
}
